package com.haoji.upload.common.threadPool;

import com.haoji.upload.common.constant.Constant;

import java.util.concurrent.*;

/**
 * @program: upload
 * @description:
 * @author: xiaolei
 * @create: 2021-04-16 10:42
 **/
public class ThreadPoolFactory {

    private static ConcurrentHashMap<String, ExecutorService> concurrentHashMap = new ConcurrentHashMap<>(16);

    static {
        ExecutorService syncPool = new ThreadPoolExecutor(3, 5, 60L, TimeUnit.SECONDS, new ArrayBlockingQueue<>(20));
        ExecutorService commonPool = new ThreadPoolExecutor(5, 10, 60L, TimeUnit.SECONDS, new ArrayBlockingQueue<>(20));
        concurrentHashMap.put(Constant.PoolType.SYNCPOOL,syncPool);
        concurrentHashMap.put(Constant.PoolType.COMMONPOOL,commonPool);
    }

    public static ExecutorService getPool(String poolName){ return concurrentHashMap.get(poolName); }

    public static void execute(Runnable runnable,String targetPool){
        ExecutorService executorService = concurrentHashMap.get(targetPool);
        executorService.execute(runnable);
    }

    public static void execute(Runnable runnable){
        ExecutorService executorService = concurrentHashMap.get(Constant.PoolType.COMMONPOOL);
        executorService.execute(runnable);
    }

    public static void execute(FutureTask<?> futureTask){
        ExecutorService executorService = concurrentHashMap.get(Constant.PoolType.COMMONPOOL);
        executorService.execute(futureTask);
    }

    public static void cancal(FutureTask<?> futureTask){ futureTask.cancel(true); }

}
